package com.wzy.mall.web.servlet.client;

import com.alibaba.druid.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wzy.mall.model.bo.clientBO.GoodsAskBO;
import com.wzy.mall.model.vo.ResultVO;
import com.wzy.mall.model.vo.clientVO.*;
import com.wzy.mall.service.client.GoodsService;
import com.wzy.mall.service.client.impl.GoodsServiceImpl;
import com.wzy.mall.utils.HTTPUtils;
import org.apache.ibatis.annotations.Param;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class Goods {

    private static GoodsService goodsService = new GoodsServiceImpl();
    private static ObjectMapper jsonMapper = new ObjectMapper();

    /**
     *  根据TypeID获取商品
     * @param request
     * @param response
     */
    public static void getGoodsByType(HttpServletRequest request, HttpServletResponse response) {
        // 获取键值对参数
        String typeId = request.getParameter("typeId");
        // 调用Service层
        List<IndexGoodsVO> indexGoodsVOList = goodsService.getGoodsByType(Integer.parseInt(typeId));
        try {
            if(indexGoodsVOList == null) {
                ResultVO error = ResultVO.error("服务器繁忙，排队去！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else {
                ResultVO ok = ResultVO.ok(indexGoodsVOList);
                response.getWriter().write(jsonMapper.writeValueAsString(ok));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  获取指定商品信息
     * @param request
     * @param response
     */
    public static void getGoodsInfo(HttpServletRequest request, HttpServletResponse response) {
        // 获取键值对参数
        String id = request.getParameter("id");
        // 调用Sercice层
        GoodsInfoVO goodsInfoVO = goodsService.getGoodsInfo(Integer.parseInt(id));
        try {
            if(goodsInfoVO == null) {
                ResultVO error = ResultVO.error("啊咧咧，服务器繁忙哦");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else {
                ResultVO ok = ResultVO.ok(goodsInfoVO);
                response.getWriter().write(jsonMapper.writeValueAsString(ok));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  获取指定商品的评论
     * @param request
     * @param response
     */
    public static void getGoodsComment(HttpServletRequest request, HttpServletResponse response) {
        // 获取键值对参数
        String goodsId = request.getParameter("goodsId");

        // 调用Service
        GoodsCommentListVO goodsCommentListVO = goodsService.getGoodsComment(Integer.parseInt(goodsId));
        try {
            if(goodsCommentListVO == null) {
                ResultVO error = ResultVO.error("啊咧咧，服务器繁忙哦");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else {
                ResultVO ok = ResultVO.ok(goodsCommentListVO);
                response.getWriter().write(jsonMapper.writeValueAsString(ok));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     *  获取留言
     * @param request
     * @param response
     */
    public static void getGoodsMsg(HttpServletRequest request, HttpServletResponse response) {
        // 请求参数是商品id
        String id = request.getParameter("id");
        // Service
        List<ReplyGetVO> replyGetVOList = goodsService.getGoodsMsg(Integer.parseInt(id));

        try {
            if(replyGetVOList == null) {
                ResultVO error = ResultVO.error("服务器抽风啦！等会儿再来吧");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else {
                ResultVO ok = ResultVO.ok(replyGetVOList);
                response.getWriter().write(jsonMapper.writeValueAsString(ok));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  根据商品ID留言
     * @param request
     * @param response
     */
    public static void askGoodsMsg(HttpServletRequest request, HttpServletResponse response) {

        try {
            // 获取JSON数据
            String requestBody = HTTPUtils.getJSONRequest(request);
            // 封装
            GoodsAskBO goodsAskBO = jsonMapper.readValue(requestBody, GoodsAskBO.class);

            // 判空
            if(StringUtils.isEmpty(goodsAskBO.getMsg())) {
                ResultVO error = ResultVO.error("你倒是问啊！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
                return;
            }

            // Service
            int result = goodsService.askGoodsMsg(goodsAskBO);
            if(result == 0) {
                ResultVO error = ResultVO.error("服务器抽风啦！等会儿再来吧");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else {
                ResultVO ok = ResultVO.ok(null);
                response.getWriter().write(jsonMapper.writeValueAsString(ok));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  搜索商品
     * @param request
     * @param response
     */
    public static void searchGoods(HttpServletRequest request, HttpServletResponse response) {
        String keyword = request.getParameter("keyword");

        // Service
        List<GoodsSearchVO> goodsSearchVOList = goodsService.searchGoods(keyword);
        try {
            if(goodsSearchVOList == null) {
                ResultVO error = ResultVO.error("服务器抽风啦！等会儿再来吧");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else {
                ResultVO ok = ResultVO.ok(goodsSearchVOList);
                response.getWriter().write(jsonMapper.writeValueAsString(ok));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
